package com.qunar.flight.myserver;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;

public class Server1 {
	/**
	 * �⼸����о���������serverֻ��һ��ssc��һ��dispatcher��������һ�������Ψһ��һ��selector����sc����ssc.accept()4�ģ��Ժ�Ķ�д���������ͨ�����淢��
	 * @param args
	 */
	public static void main(String[]args){
		try {
			Selector sel=Selector.open();
			
			ServerSocketChannel ssc=ServerSocketChannel.open();//���ssc
			ssc.socket().setReuseAddress(true);
			ssc.socket().bind(new InetSocketAddress(8000));
			ssc.configureBlocking(false);
			
			Dispatcher d = new Dispatcher1(sel);//���dispatcher��ͬʱʹ֮����һ��selector			
			d. register(ssc, SelectionKey.OP_ACCEPT, new AcceptHandler(ssc,d));//
//			1.��ssc����ע��OP_ACCEPT�¼���ServerSocketChannelҲֻ��ע�������¼������AbstractSelectableChannel��Line195;
//			2.ָ�������¼��Ĵ�����AcceptHandler
//			3.��selector��ssc��
			new Thread(d).start();
/**********************************�����㵰������********************************************/			
			ServerSocketChannel ssc2=ServerSocketChannel.open();//���ssc
			ssc2.socket().setReuseAddress(true);
			ssc2.socket().bind(new InetSocketAddress(9000));
			ssc2.configureBlocking(false);
			
			sel=Selector.open();
			Dispatcher d2 = new Dispatcher1(sel);//���dispatcher��ͬʱʹ֮����һ��selector			
			d2. register(ssc2, SelectionKey.OP_ACCEPT, new AcceptHandler2(ssc2,d2));//
//			1.��ssc����ע��OP_ACCEPT�¼���ServerSocketChannelҲֻ��ע�������¼������AbstractSelectableChannel��Line195;
//			2.ָ�������¼��Ĵ�����AcceptHandler
//			3.��selector��ssc��
			new Thread(d2).start();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
